using System;
using System.Linq.Expressions;
using System.Reflection;
using Magiq.Support;

namespace Magiq.Update {
  public class PropertySet : IPropertySet {
    private readonly PropertyInfo propertyInfo;

    public PropertySet(LambdaExpression property, LambdaExpression value) {
      ValueExpression = value;
      propertyInfo = property.ToPropertyInfo();
      PropertyExpression = property;
    }

    #region IPropertySet Members

    public string PropertyName {
      get { return propertyInfo.Name; }
    }

    public LambdaExpression ValueExpression { get; private set; }

    public Type PropertyType {
      get { return propertyInfo.PropertyType; }
    }

    public LambdaExpression PropertyExpression { get; private set; }

    public Type DeclaringType {
      get { return propertyInfo.DeclaringType; }
    }

    public Type ValueType {
      get { return ValueExpression.Body.Type; }
    }

    public string ParentKey {
      get { return PropertyExpression.PreviousExpressionChain(true).ToString().From("."); }
    }

    #endregion
  }
}